package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * @author げっしょく
 * @date 2021/9/9
 */


@WebServlet("/brand/*")
public class BrandServletext extends BrandServlet{

    private BrandService brandService = new BrandServiceImpl();

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BufferedReader reader = request.getReader();
        String s = reader.readLine().toString();

        Brand brand = JSON.parseObject(s, Brand.class);
        System.out.println(brand);


        BrandServiceImpl brandService = new BrandServiceImpl();

        brandService.insert(brand);

        response.getWriter().write("true");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");

        System.out.println(id);
        brandService.delByIdAfter(Integer.parseInt(id));

        response.getWriter().write("true");

    }


    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BufferedReader reader = request.getReader();
        String s = reader.readLine().toString();

        Brand brand = JSON.parseObject(s, Brand.class);

//        System.out.println(brand);


        brandService.update(brand);

        response.getWriter().write("true");



    }
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取从数据库读取的数据
        List<Brand> brands = brandService.selectAll();
        //将对象转换成JSON
        String s = JSON.toJSONString(brands);

        //该响应数据的编码格式改为utf-8
        response.setContentType("text/json;charset=utf-8");
        //将数据响应给浏览器
        response.getWriter().write(s);
    }

    public void delarray(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BufferedReader reader = request.getReader();
        String s = reader.readLine();

        int[] ids = JSON.parseObject(s, int[].class);
        System.out.println(Arrays.toString(ids));

        brandService.delarray(ids);

        response.getWriter().write("true");

    }

    public void selectPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pageSize1 = request.getParameter("pageSize");
        String currentPage1 = request.getParameter("currentPage");

        int pageSize = Integer.parseInt(pageSize1);
        System.out.println(pageSize);
        int currentPage = Integer.parseInt(currentPage1);
        System.out.println(currentPage);

        int indxt = (currentPage - 1 ) * pageSize;

        System.out.println(pageSize);
        System.out.println(indxt);


        List<Brand> brands = brandService.selectByPage(indxt, pageSize);

        int i = brandService.selectCount();

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(brands);
        pageBean.setCount(i);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=utf-8");

        response.getWriter().write(s);

    }


    public void selectPageAndCond(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pageSize1 = request.getParameter("pageSize");
        String currentPage1 = request.getParameter("currentPage");

        int pageSize = Integer.parseInt(pageSize1);

        int currentPage = Integer.parseInt(currentPage1);

        int index = (currentPage - 1 ) * pageSize;

        System.out.println(pageSize);
        System.out.println(index);

        BufferedReader reader = request.getReader();
        String s1 = reader.readLine();
        Brand brand = JSON.parseObject(s1, Brand.class);

        String brandName = brand.getBrandName();
        brandName = "%"+ brandName+"%";
        brand.setBrandName(brandName);

        String companyName = brand.getCompanyName();
        companyName = "%" + companyName + "%";
        brand.setCompanyName(companyName);
        System.out.println(companyName);
        System.out.println(brandName);



        List<Brand> brands = brandService.selectByPageAndcond(index, pageSize, brand);
        int count = brandService.selectCountAndcond(brand);

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(brands);
        pageBean.setCount(count);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=utf-8");

        response.getWriter().write(s);

    }

}
